package com.qianli.cashmama.product.server.entity;

import com.fqgj.common.entity.BaseEntity;
import com.qianli.cashmama.product.server.domain.BusinessRuleDetailDomain;
import com.qianli.cashmama.product.server.exception.ProductException;
import com.qianli.cashmama.product.facade.enums.BusinessRuleEnum;
import com.qianli.cashmama.product.facade.enums.ErrorCodeEnum;
import com.qianli.cashmama.product.facade.enums.NecessaryEnum;
import com.qianli.cashmama.product.facade.enums.RuleOwnerTypeEnum;

/**
 * Created by xuyang on 2017/7/19.
 */

public class ProductRuleDetailEntity extends BaseEntity{
    private static final long serialVersionUID = 570333173390750637L;
    //规则详情中文描述
    private String describe;

    //产品规则名称
    /** @see BusinessRuleEnum code */
    private String productRule;

    //解析规则用的参数
    private String productRuleValue;


    //是否必须
    /** @see NecessaryEnum */
    private Integer isNecessary;

    //所有者类型
    private String ownerType;

    //所有者编码
    private String ownerCode;

    public BusinessRuleDetailDomain convert() {
        BusinessRuleDetailDomain domain = new BusinessRuleDetailDomain();
        domain.setDescribe(this.describe);
        BusinessRuleEnum businessRuleEnum = BusinessRuleEnum.getByCode(this.productRule);
        if (businessRuleEnum == null) {
            throw new ProductException(ErrorCodeEnum.INVALID_PRODUCT_RULE);
        }
        domain.setBusinessRuleEnum(businessRuleEnum);
        domain.setProductRuleValue(this.productRuleValue);
        NecessaryEnum necessary = isNecessary == NecessaryEnum.NECESSARY.getStatus() ? NecessaryEnum.NECESSARY : NecessaryEnum.OPTIONAL;
        domain.setIsNecessary(necessary);
        RuleOwnerTypeEnum ownerType = RuleOwnerTypeEnum.getByCode(this.ownerType);
        if (ownerType == null) {
            throw new ProductException(ErrorCodeEnum.INVALID_RULE_OWNER_TYPE);
        }
        domain.setOwnerType(ownerType);
        domain.setOwnerCode(ownerCode);
        return domain;
    }


    public String getDescribe() {
        return describe;
    }

    public ProductRuleDetailEntity setDescribe(String describe) {
        this.describe = describe;
        return this;
    }

    public String getProductRule() {
        return productRule;
    }

    public ProductRuleDetailEntity setProductRule(String productRule) {
        this.productRule = productRule;
        return this;
    }

    public String getProductRuleValue() {
        return productRuleValue;
    }

    public ProductRuleDetailEntity setProductRuleValue(String productRuleValue) {
        this.productRuleValue = productRuleValue;
        return this;
    }

    public Integer getIsNecessary() {
        return isNecessary;
    }

    public ProductRuleDetailEntity setIsNecessary(Integer isNecessary) {
        this.isNecessary = isNecessary;
        return this;
    }

    public String getOwnerType() {
        return ownerType;
    }

    public ProductRuleDetailEntity setOwnerType(String ownerType) {
        this.ownerType = ownerType;
        return this;
    }

    public String getOwnerCode() {
        return ownerCode;
    }

    public ProductRuleDetailEntity setOwnerCode(String ownerCode) {
        this.ownerCode = ownerCode;
        return this;
    }
}


